﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;

namespace TowerDefence
{
    public class PlayerDataManager : MonoBehaviour
    {
        #region 单例模式

        // 确保只有一个 PlayerDataManager 实例
        public static PlayerDataManager instance;

        private void Awake()
        {
            // 如果已经存在实例，则输出错误信息并返回
            if (instance != null)
            {
                Debug.LogError("场景中存在多个 PlayerDataManager");
                return;
            }
            // 将当前实例赋给 instance
            instance = this;
        }

        #endregion

        MainMenuManager main;

        private Scene currentScene;

        public static bool TutorialFinished;

        [Header("货币")]
        public static int Money;
        public static int Gem;

        [Header("音频")]
        [Range(0, 1)]
        public static float SoundValue;
        [Range(0, 1)]
        public static float MusicValue;

        [Header("敌人日志信息")]
        public static int EnemyLogIndex;

        [Header("商店物品")]
        public static int GoldItemAmount;
        public static int HealthItemAmount;
        public static int BombItemAmount;
        public static int FreezeTimeItemAmount;

        [Header("塔升级索引")]
        public static int arrowTowerIndex01;
        public static int arrowTowerIndex02;
        public static int arrowTowerIndex03;
        public static int fireTowerIndex01;
        public static int fireTowerIndex02;
        public static int fireTowerIndex03;
        public static int cannonTowerIndex01;
        public static int cannonTowerIndex02;
        public static int cannonTowerIndex03;
        public static int poisonTowerIndex01;
        public static int poisonTowerIndex02;
        public static int poisonTowerIndex03;

        [Header("关卡进度")]
        public static List<int> starScoresPerLevel = new List<int>();
        public static int _levelsUnlocked = 1;

        public TowerData[] towerDatasToReset;
        public TowerUpgradeShopButton[] towerUpgradeShopButtons;
        [Space]
        public TowerUpgradeShopButton[] initCrossbowButtonIndexes;
        public TowerUpgradeShopButton[] initFireButtonIndexes;
        public TowerUpgradeShopButton[] initCannonButtonIndexes;
        public TowerUpgradeShopButton[] initPoisonButtonIndexes;

        public static List<TowerUpgradeShopButton> crossbowButtonIndexes = new List<TowerUpgradeShopButton>(3);
        public static List<TowerUpgradeShopButton> fireButtonIndexes = new List<TowerUpgradeShopButton>(3);
        public static List<TowerUpgradeShopButton> cannonButtonIndexes = new List<TowerUpgradeShopButton>(3);
        public static List<TowerUpgradeShopButton> poisonButtonIndexes = new List<TowerUpgradeShopButton>(3);

        void Start()
        {
            // 获取主菜单管理器的实例
            main = MainMenuManager.instance;

            // 如果交叉弓箭按钮索引为空，则使用初始值填充
            if (crossbowButtonIndexes.Count <= 0)
            {
                for (int i = 0; i < initCrossbowButtonIndexes.Length; i++)
                {
                    crossbowButtonIndexes.Add(initCrossbowButtonIndexes[i]);
                    fireButtonIndexes.Add(initFireButtonIndexes[i]);
                    cannonButtonIndexes.Add(initCannonButtonIndexes[i]);
                    poisonButtonIndexes.Add(initPoisonButtonIndexes[i]);
                }
            }

            // 加载玩家进度
            LoadProgress();

            // 更新货币显示
            main.UpdateCurrency();

            // 保存当前进度
            SaveProgress(this);

            // 获取当前场景
            currentScene = SceneManager.GetActiveScene();
        }

        // 保存玩家进度的方法
        public void SaveProgress(PlayerDataManager data)
        {
            SaveSystem.SaveData(data);
        }

        // 加载玩家进度的方法
        public void LoadProgress()
        {
            // 从保存系统中加载数据
            PlayerData data = SaveSystem.LoadData();

            // 设置教程完成状态
            TutorialFinished = data.tutorialFinished;

            // 设置货币进度
            Money = data.money;
            Gem = data.gem;

            // 设置音效和音乐音量
            SoundValue = data.soundValue;
            MusicValue = data.musicValue;

            // 设置敌人日志索引
            EnemyLogIndex = data.enemyLogIndex;

            // 设置物品数量
            GoldItemAmount = data.goldItemAmount;
            HealthItemAmount = data.goldItemAmount; // 这里应该是 HealthItemAmount = data.healthItemAmount;
            BombItemAmount = data.bombItemAmount;
            FreezeTimeItemAmount = data.freezeTimeItemAmount;

            // 设置游戏关卡进度
            _levelsUnlocked = data.levelsUnlocked;

            // 如果关卡星级数据为空，则返回
            if (data.levelStars == null) return;

            // 初始化星级列表并赋值
            starScoresPerLevel = null;
            starScoresPerLevel = new List<int>();
            for (int i = 0; i < data.levelStars.Count; i++)
            {
                starScoresPerLevel.Add(data.levelStars[i]);
            }

            // 塔升级进度
            arrowTowerIndex01 = data.arrowTowerIndex01;  // 箭塔等级索引01
            arrowTowerIndex02 = data.arrowTowerIndex02;  // 箭塔等级索引02
            arrowTowerIndex03 = data.arrowTowerIndex03;  // 箭塔等级索引03
            fireTowerIndex01 = data.fireTowerIndex01;    // 火塔等级索引01
            fireTowerIndex02 = data.fireTowerIndex02;    // 火塔等级索引02
            fireTowerIndex03 = data.fireTowerIndex03;    // 火塔等级索引03
            cannonTowerIndex01 = data.cannonTowerIndex01;  // 炮塔等级索引01
            cannonTowerIndex02 = data.cannonTowerIndex02;  // 炮塔等级索引02
            cannonTowerIndex03 = data.cannonTowerIndex03;  // 炮塔等级索引03
            poisonTowerIndex01 = data.poisonTowerIndex01;  // 毒塔等级索引01
            poisonTowerIndex02 = data.poisonTowerIndex02;  // 毒塔等级索引02
            poisonTowerIndex03 = data.poisonTowerIndex03;  // 毒塔等级索引03

            // 更新塔按钮索引和用户界面
            crossbowButtonIndexes[0].buttonIndex = arrowTowerIndex01;  // 弩按钮索引0对应箭塔等级索引01
            crossbowButtonIndexes[1].buttonIndex = arrowTowerIndex02;  // 弩按钮索引1对应箭塔等级索引02
            crossbowButtonIndexes[2].buttonIndex = arrowTowerIndex03;  // 弩按钮索引2对应箭塔等级索引03
            fireButtonIndexes[0].buttonIndex = fireTowerIndex01;        // 火按钮索引0对应火塔等级索引01
            fireButtonIndexes[1].buttonIndex = fireTowerIndex02;        // 火按钮索引1对应火塔等级索引02
            fireButtonIndexes[2].buttonIndex = fireTowerIndex03;        // 火按钮索引2对应火塔等级索引03
            cannonButtonIndexes[0].buttonIndex = cannonTowerIndex01;    // 炮按钮索引0对应炮塔等级索引01
            cannonButtonIndexes[1].buttonIndex = cannonTowerIndex02;    // 炮按钮索引1对应炮塔等级索引02
            cannonButtonIndexes[2].buttonIndex = cannonTowerIndex03;    // 炮按钮索引2对应炮塔等级索引03
            poisonButtonIndexes[0].buttonIndex = poisonTowerIndex01;    // 毒按钮索引0对应毒塔等级索引01
            poisonButtonIndexes[1].buttonIndex = poisonTowerIndex02;    // 毒按钮索引1对应毒塔等级索引02
            poisonButtonIndexes[2].buttonIndex = poisonTowerIndex03;    // 毒按钮索引2对应毒塔等级索引03

            for (int i = 0; i < towerUpgradeShopButtons.Length; i++)
            {
                towerUpgradeShopButtons[i].UpdateBaseTextUI();  // 更新塔升级商店按钮的基本文本UI
            }
        }

            // 重置进度按钮
            public void ResetProgressButton()
            {
                SaveSystem.DeleteData();  // 删除保存的数据
            #region 进度重置
            ResetScriptableObjects();  // 重置可脚本化对象

                TutorialFinished = false;  // 教程未完成
                EnemyLogIndex = 0;  // 敌人日志索引为0

                // 玩家货币重置
                Money = 0;  // 金币为0
                Gem = 0;    // 宝石为0

                SoundValue = 1;  // 音效值为1
                MusicValue = 1;  // 音乐值为1

                // 玩家等级进度重置
                _levelsUnlocked = 1;  // 解锁的关卡为1
                starScoresPerLevel = null;  // 星级得分列表为空
                starScoresPerLevel = new List<int>();  // 创建新的星级得分列表

            // 商店物品重置
            GoldItemAmount = 0;                 // 金币物品数量重置
            BombItemAmount = 1;                 // 炸弹物品数量重置
            HealthItemAmount = 0;               // 生命物品数量重置
            FreezeTimeItemAmount = 0;           // 冻结时间物品数量重置

            // 塔升级索引重置
            arrowTowerIndex01 = 0;               // 箭塔升级索引01重置
            arrowTowerIndex02 = 0;               // 箭塔升级索引02重置
            arrowTowerIndex03 = 0;               // 箭塔升级索引03重置
            fireTowerIndex01 = 0;                // 火塔升级索引01重置
            fireTowerIndex02 = 0;                // 火塔升级索引02重置
            fireTowerIndex03 = 0;                // 火塔升级索引03重置
            cannonTowerIndex01 = 0;              // 加农炮塔升级索引01重置
            cannonTowerIndex02 = 0;              // 加农炮塔升级索引02重置
            cannonTowerIndex03 = 0;              // 加农炮塔升级索引03重置
            poisonTowerIndex01 = 0;              // 毒塔升级索引01重置
            poisonTowerIndex02 = 0;              // 毒塔升级索引02重置
            poisonTowerIndex03 = 0;              // 毒塔升级索引03重置

            // 塔升级按钮索引重置
            for (int i = 0; i < crossbowButtonIndexes.Count; i++)
            {
                crossbowButtonIndexes[i].buttonIndex = 0;  // 弩塔升级按钮索引重置
            }
            for (int i = 0; i < fireButtonIndexes.Count; i++)
            {
                fireButtonIndexes[i].buttonIndex = 0;      // 火塔升级按钮索引重置
            }
            for (int i = 0; i < cannonButtonIndexes.Count; i++)
            {
                cannonButtonIndexes[i].buttonIndex = 0;    // 加农炮塔升级按钮索引重置
            }
            for (int i = 0; i < poisonButtonIndexes.Count; i++)
            {
                poisonButtonIndexes[i].buttonIndex = 0;   // 毒塔升级按钮索引重置
            }
            #endregion

            UpdateCurrency();                       // 更新货币

            SaveProgress(this);                     // 保存进度

            SceneManager.LoadScene(currentScene.buildIndex);   // 加载当前场景

        }

        // 更新货币信息
        public void UpdateCurrency()
        {
            main.moneyText.text = Money.ToString();  // 更新金币文本信息
            main.gemText.text = Gem.ToString();      // 更新宝石文本信息
        }

        // 重置ScriptableObject对象
        void ResetScriptableObjects()
        {
            for (int i = 0; i < towerDatasToReset.Length; i++)
            {
                towerDatasToReset[i].upgrade01 = null;  // 重置塔升级数据01
                towerDatasToReset[i].upgrade02 = null;  // 重置塔升级数据02
            }
        }

        // 当应用程序暂停时调用
        public void OnApplicationPause(bool pause)
        {
            if (pause)
                SaveProgress(this);  // 在暂停时保存进度
        }

        // 当应用程序退出时调用
        public void OnApplicationQuit()
        {
            SaveProgress(this);    // 在退出时保存进度
        }

    }
}